//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.fufeiii.bt.nomiddleware.print.controller;

import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class AESEncryptUtils {
    private static final String ENCRYPTION_MODE = "AES";
    private static final Logger logger = LoggerFactory.getLogger(AESEncryptUtils.class);

    public AESEncryptUtils() {
    }

    public static String encrypt(String content, String encryptKey) {
        try {
            SecretKey key = generateMySQLAESKey(encryptKey, StandardCharsets.US_ASCII);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, key);
            byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            return Base64.encodeBase64String(b);
        } catch (Exception var5) {
            logger.error("加密失败，原文：{} e:", content, var5);
            throw new RuntimeException(var5.getMessage(), var5);
        }
    }

    public static String decrypt(String encryptStr, String decryptKey) {
        try {
            SecretKey key = generateMySQLAESKey(decryptKey, StandardCharsets.US_ASCII);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] encryptBytes = Base64.decodeBase64(encryptStr);
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        } catch (Exception var6) {
            logger.error("解密失败，原文：{} e:", encryptStr, var6);
            throw new RuntimeException(var6.getMessage(), var6);
        }
    }

    private static SecretKeySpec generateMySQLAESKey(String key, Charset charset) {
        try {
            byte[] finalKey = new byte[16];
            int i = 0;
            byte[] var4 = key.getBytes(charset);
            int var5 = var4.length;

            for (int var6 = 0; var6 < var5; ++var6) {
                byte b = var4[var6];
                int var10001 = i++;
                finalKey[var10001 % 16] ^= b;
            }

            return new SecretKeySpec(finalKey, "AES");
        } catch (Exception var8) {
            throw new RuntimeException(var8.getMessage(), var8);
        }
    }

    public static void main(String[] args) {
        String decrypt = decrypt("1/jHA3jJaYa+zLHOrbmGeg==", "UAT_AAAAAAAAAAAA");
        System.out.println(decrypt);
    }

}
